package com.example.cardgame

import android.content.Intent
import android.content.SharedPreferences
import android.net.Uri
import android.os.Bundle
import android.provider.MediaStore
import android.widget.Button
import android.widget.ImageView
import android.widget.TextView
import androidx.activity.enableEdgeToEdge
import androidx.appcompat.app.AppCompatActivity

class SecondActivity : AppCompatActivity() {
    private lateinit var sharedPreferences: SharedPreferences
    private lateinit var userInfoTextView: TextView
    private lateinit var userAvatar: ImageView

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        enableEdgeToEdge()
        setContentView(R.layout.activity_second)

        sharedPreferences = getSharedPreferences("user_info", MODE_PRIVATE)
        userInfoTextView = findViewById(R.id.user_info)
        userAvatar = findViewById(R.id.user_avatar)

        val modifyInfoButton: Button = findViewById(R.id.modify_info_button)
        val playGameButton: Button = findViewById(R.id.play_game_button)

        modifyInfoButton.setOnClickListener {
            val intent = Intent(this, ThirdActivity::class.java)
            startActivity(intent)
        }

        playGameButton.setOnClickListener {
            val intent = Intent(this, MainActivity::class.java)
            startActivity(intent)
        }

        updateUserInfo()
    }

    override fun onResume() {
        super.onResume()
        updateUserInfo()
    }

    private fun updateUserInfo() {
        val username = sharedPreferences.getString("username", "")
        val sex = sharedPreferences.getString("sex", "")
        val number = sharedPreferences.getString("number", "")
        val phone = sharedPreferences.getString("phone", "")
        val email = sharedPreferences.getString("email", "")
        val hobby = sharedPreferences.getString("hobby", "")
        val score = sharedPreferences.getInt("score", 0)
        val avatar = sharedPreferences.getString("avatar", "")

        userInfoTextView.text = "姓名: $username\n\n性别: $sex\n\n学号: $number\n\n电话: $phone\n\n邮件地址: $email\n\n爱好: $hobby\n\n游戏最高分: $score"

        if (avatar != null && avatar.isNotEmpty()) {
            val bitmap = MediaStore.Images.Media.getBitmap(contentResolver, Uri.parse(avatar))
            userAvatar.setImageBitmap(bitmap)
        }
    }
}